class Solution:
    def maxSubarraySumCircular(self, A: list) -> int:
        max_sum = A[0]
        cur_sum1 = A[0]
        min_sum = A[0]
        cur_sum2 = A[0]
        for i in range(1, len(A)):
            cur_sum1 = max(A[i], cur_sum1 + A[i])
            max_sum = max(cur_sum1, max_sum)

            cur_sum2 = min(A[i], cur_sum2 + A[i])
            min_sum = min(cur_sum2, min_sum)

        total = sum(A)
        if total < 0 and max_sum < 0:
            return max_sum

        return max(max_sum, total - min_sum)
